package com.winchain.chainbill.tbill.biz.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.basic.context.TimeoutOperation;
import com.winchain.chainbill.basic.invoice.rx.TBillInvoiceDfrUploadUpdateSubscriber;
import com.winchain.chainbill.serfee.rx.TBillDctUpdateSerFeeSubscriber;
import com.winchain.chainbill.tbill.base.dao.TbRecDao;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbOpt;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.service.TbDctService;
import com.winchain.chainbill.tbill.base.service.TbRecService;
import com.winchain.chainbill.tbill.basic.rx.TBillMoveSubscriber;
import com.winchain.chainbill.tbill.basic.rx.TBillOptSubscriber;
import com.winchain.chainbill.tbill.basic.service.TBillTimeoutService;
import com.winchain.chainbill.tbill.biz.TBill;
import com.winchain.chainbill.tbill.biz.TBillEvent;
import com.winchain.chainbill.tbill.biz.rx.TBillDctDebitContractSponsorSubcriber;
import com.winchain.chainbill.tbill.biz.rx.TBillDctDebitProtoSignSubscriber;
import com.winchain.chainbill.tbill.biz.rx.TBillDctPayInsMsgSubscriber;
import com.winchain.chainbill.tbill.biz.rx.TBillDctQuotaSubscriber;
import com.winchain.chainbill.tbill.biz.rx.TBillDctSubscriber;
import com.winchain.chainbill.tbill.biz.rx.TBillIntPayContractSponsorSubcriber;
import com.winchain.chainbill.tbill.biz.rx.TBillIntPayProtoSignSubscriber;
import com.winchain.chainbill.tbill.biz.rx.TBillRjctUpdateAttSubscriber;
import com.winchain.chainbill.tbill.biz.rx.TBillServicePayProtoSignSubscriber;
import com.winchain.chainbill.tbill.biz.rx.TBillServicePaySponsorSubscriber;
import com.winchain.chainbill.tbill.biz.service.TBillPayInsService;
import com.winchain.chainbill.tbill.biz.vo.TbRecVo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service
public class TBillPayInsServiceImpl implements TBillPayInsService {
	@Autowired
	private TbRecDao tbRecDao;
	@Autowired
	private TbDctService tbDctService;
	@Autowired
	private TbRecService tbRecService;
	@Autowired
	private TBillOptSubscriber tBillOptSubscriber;
	@Autowired
	private TBillMoveSubscriber tBillMoveSubscriber;
	@Autowired
	private TBillDctSubscriber tBillDctSubscriber;
	@Autowired
	private TBillDctPayInsMsgSubscriber tBillDctPayInsMsgSubscriber;
	@Autowired
	private TBillDctQuotaSubscriber tBillDctQuotaSubscriber;
	@Autowired
	private TBillDctUpdateSerFeeSubscriber tBillDctUpdateSerFeeSubscriber;
	@Autowired
	private TBillIntPayContractSponsorSubcriber tBillIntPayContractSponsorSubcriber;
	@Autowired
	private TBillIntPayProtoSignSubscriber tBillIntPayProtoSignSubscriber;
	@Autowired
	private TBillServicePaySponsorSubscriber tBillServicePaySponsorSubscriber;
	@Autowired
	private TBillServicePayProtoSignSubscriber tBillServicePayProtoSignSubscriber;
	@Autowired
	private TBillTimeoutService tBillTimeoutService;
	@Autowired
	private TBillRjctUpdateAttSubscriber tBillRjctUpdateAttSubscriber;
	@Autowired
	private TBillInvoiceDfrUploadUpdateSubscriber tBillInvoiceDfrUploadUpdateSubscriber;
	@Autowired
	private TBillDctDebitContractSponsorSubcriber tBillDctDebitContractSponsorSubcriber;
	@Autowired
	private TBillDctDebitProtoSignSubscriber tBillDctDebitProtoSignSubscriber;

	@Pagination
	public List<TbRecVo> queryPayInsList(PageLimit limit, String cusName, String billCode) {
		String billState = "S003";
		if (StringUtil.notEmpty(cusName)) {
			cusName = StringUtil.getParameterLike(cusName);
		}
		if (StringUtil.notEmpty(billCode)) {
			billCode = StringUtil.getParameterLike(billCode);
		}
		String buyerId = CommonDataUtil.getCurEntId();
		return this.tbRecDao.queryDctList(billState, cusName, buyerId, billCode);
	}

	public RetMsg pass(String dctId, String location, String phoneCode) {
		TbDct dct = (TbDct) this.tbDctService.selectByKey(dctId);
		TbRec rec = (TbRec) this.tbRecService.selectByKey(dct.getBillId());
		TbOpt opt = getOpt(rec);
		opt.setOptCode("E004");
		opt.setRemark("������������");

		String timeOutMsg = "������������,������������";
		TimeoutOperation timeoutOpt = new TimeoutOperation();
		timeoutOpt.setOptCode("E006");
		timeoutOpt.setRemark(timeOutMsg);
		boolean timeOut = this.tBillTimeoutService.check(rec, opt, timeoutOpt);
		if (timeOut) {
			reject(rec, dct, opt);
			return new RetMsg("warning", false, timeOutMsg);
		}
		TBill<TbDct> bill = new TBill();
		bill.setSource(dct);
		bill.setRec(rec);
		TBillEvent<TbDct> event = new TBillEvent(bill);
		event.setOperation(opt);
		event.setTimeoutOpt(timeoutOpt);
		event.setValue("phoneCode", phoneCode);
		event.setValue("location", location);
		Observable<TBillEvent<TbDct>> observers = Observable.just(event);

		observers.subscribe(this.tBillDctDebitContractSponsorSubcriber);
		observers.subscribe(this.tBillDctDebitProtoSignSubscriber);

		observers.subscribe(this.tBillIntPayContractSponsorSubcriber);
		observers.subscribe(this.tBillIntPayProtoSignSubscriber);
		observers.subscribe(this.tBillServicePaySponsorSubscriber);
		observers.subscribe(this.tBillServicePayProtoSignSubscriber);
		observers.subscribe(this.tBillOptSubscriber);
		observers.subscribe(this.tBillMoveSubscriber);
		observers.subscribe(this.tBillDctSubscriber);
		observers.subscribe(this.tBillDctPayInsMsgSubscriber);
		return new RetMsg("success", false, "��������������");
	}

	private void reject(TbRec rec, TbDct dct, TbOpt opt) {
		dct.setDctState("0");
		TBill<TbDct> bill = new TBill();
		bill.setSource(dct);
		bill.setRec(rec);
		TBillEvent<TbDct> event = new TBillEvent(bill);
		event.setOperation(opt);
		event.setValue("quotaOptType", "qt_ubk");
		event.setValue("serFeeBizState", "D");
		event.setValue("dfrState", "D");
		Observable<TBillEvent<TbDct>> observers = Observable.just(event);
		observers.subscribe(this.tBillOptSubscriber);
		observers.subscribe(this.tBillMoveSubscriber);
		observers.subscribe(this.tBillDctSubscriber);
		observers.subscribe(this.tBillDctQuotaSubscriber);
		observers.subscribe(this.tBillDctUpdateSerFeeSubscriber);
		observers.subscribe(this.tBillRjctUpdateAttSubscriber);
		observers.subscribe(this.tBillInvoiceDfrUploadUpdateSubscriber);
		observers.subscribe(this.tBillDctPayInsMsgSubscriber);
	}

	private TbOpt getOpt(TbRec rec) {
		TbOpt opt = new TbOpt();
		opt.setCurState(rec.getBillState());
		opt.setLimDate(rec.getExpDate());
		return opt;
	}

	public RetMsg reject(String dctId, String remark) {
		TbDct dct = (TbDct) this.tbDctService.selectByKey(dctId);
		TbRec rec = (TbRec) this.tbRecService.selectByKey(dct.getBillId());
		TbOpt opt = getOpt(rec);
		opt.setOptCode("E005");
		opt.setRemark(remark);
		reject(rec, dct, opt);
		return new RetMsg("success", false, "����������");
	}

	public int getMsgCountForTBillPayIns(String cusId) {
		String billState = "S003";
		return this.tbRecDao.getMsgCountForTBillPayIns(billState, cusId);
	}
}
